package com.xunyji.xunyjitest.service;

import com.xunyji.xunyjitest.comm.TransformUtils;
import com.xunyji.xunyjitest.comm.util.weixin.MessageUtils;
import com.xunyji.xunyjitest.model.weixin.receive.ReceiveTextMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * @author AltEnter
 * @create 2019-01-09 23:00
 * @desc 接收消息后的业务处理服务层实现类
 **/
@Service
@Slf4j
public class ReceiveMessageServiceImpl implements ReceiveMessageService {

    @Autowired
    private MessageUtils messageUtils;

    @Override
    public String textMessageHandler(Map<String, String> receiveParam) throws UnsupportedEncodingException {
//        01 用于存放封装好的回复文本消息的XML数据
        String message = null;
//        02 将Map类型参数转化成ReceiveTextMessage类型
        ReceiveTextMessage receiveTextMessage = TransformUtils.parseMap2Object(receiveParam, ReceiveTextMessage.class);
        log.info("接收到的text消息请求参数为：" + receiveTextMessage);

//        03 获取文本内容和双方信息
//        0301 粉丝appId
        String fromUserName = receiveTextMessage.getFromUserName();
//        0302 公众号appId
        String toUserName = receiveTextMessage.getToUserName();
//        0303 接收到的文本内容
        String content = receiveTextMessage.getContent();
        content = content.trim();

//        04 根据文本内容响应不同的数据 TODO: 应该将接收到的消息信息存储到数据库或者缓存中
        if ("1".equals(content)) {
            message = messageUtils.initReplyMenuTextMessage(fromUserName, toUserName, messageUtils.firstMenu());
        } else if ("2".equals(content)) {
            message = messageUtils.initReplyMenuTextMessage(fromUserName, toUserName, messageUtils.secondMenu());
        } else if ("?".equals(content) || "？".equals(content)) {
            message = messageUtils.initReplyMenuTextMessage(fromUserName, toUserName, messageUtils.menuText());
        } else if (content.startsWith("en翻译") || content.startsWith("zh翻译")) {
            if (content.startsWith("en翻译")) {
                String word = content.replaceAll("^en翻译", "").trim();
                if("".equals(word)){
                    message = messageUtils.initReplyMenuTextMessage(fromUserName, toUserName, messageUtils.threeMenu());
                }else{
                    message = messageUtils.initReplyTransTextMessage(fromUserName, toUserName, messageUtils.getTransStrResult(word, "auto", "en"));
                }
            }
            if (content.startsWith("zh翻译")) {
                String word = content.replaceAll("^zh翻译", "").trim();
                if("".equals(word)){
                    message = messageUtils.initReplyMenuTextMessage(fromUserName, toUserName, messageUtils.threeMenu());
                }else{
                    message = messageUtils.initReplyTransTextMessage(fromUserName, toUserName, messageUtils.getTransStrResult(word, "auto", "zh"));
                }
            }
        }
        else {
            message = messageUtils.initReplyTextMessage(fromUserName, toUserName, content);
        }

        return message;
    }
}

